// Show the full 'new note' form after pag has finished loading
// note_switch_form('expand')

// Display all notes in main placeholder
//note_all() // show all notes on first page
note_range(0,10) // show last 10 notes on first page
//note_range(0,3) // show last 3 notes on first page

//--------------------------------------

function note_all()
{
	$.ajax({
		type: "POST",
		url: "controllers/notes_controller.php",
		data: {action:'show'},
		success: function(msg){
	    	$("#page").html(msg)
		}
	});
}

function note_range(start,limit)
{		
	$.ajax({
		type: "POST",
		url: "controllers/notes_controller.php",
		data: {action:'show',param:'range',start:start,limit:limit},
		success: function(msg){
	    	$("#page").html(msg)
		}
	});
}

function note_combo()
{
	var ar = $('#pagination_combo').val().split(',')
	note_range(ar[0],ar[1])
}

function note_full(id)
{
	$.ajax({
		type: "POST",
		url: "controllers/notes_controller.php",
		data: {action:'show', param:'full', id:id},
		success: function(msg){
			$("#page").html(msg)
		}
	});
}

function note_new()
{
	var title =	$('#note_new_title').val()
	var body =	$('#note_new_body').val()
	
	$.ajax({
		type: "POST",
		url: "controllers/notes_controller.php",
		data: {action:'new', title:title, body:body},
		success: function(new_note_id){
			$('#note_new_title').val('')
			$('#note_new_body').val('')
			
			// Refresh list : add this note
			$.ajax({
				type: "POST",
				url: "controllers/notes_controller.php",
				data: {action:'show', param:'small', id:new_note_id},
				success: function(msg){
					// BUG : $('#page').prepend(msg).fadeIn('slow') should work but it doesn't
					// even when the prepended div id hidden (display:none) -- jQuery 1.4.2
					$('#page').prepend(msg)
					$('#page').children(':first').slideDown('fast')
				}
			});
		}
	 });	
}

function note_edit_title(block)
{
	var id_all = $(block).parent().parent().attr('id').split('_') // gets record id from parent container id
	var id = id_all[2] // id of the record in database
	
	var content = trim($(block).html()) // gets content
	
	var newform = "<input type=\"text\" id=\"nste_"+id+"\" " // nste = note_small_title_edit
				+ "value=\""+content+"\" />&nbsp;"
				+ "<input type=\"submit\" value=\"Save &#x2192;\" "
				+ "onclick=\"note_save_title('"+id+"')\">"
		
	$(block).replaceWith(newform)
}

function note_save_title(id)
{
	var content = trim($('#nste_'+id).val())
	
	$.ajax({
		type: "POST",
		url: "controllers/notes_controller.php",
		data: {action:'field_update', id:id, field:'title', value:content},
		success: function(msg){
			var output = "<span onclick=\"note_edit_title(this)\">"+msg+"</span>"
			$('#ns_title_'+id).html(output)
		}
	 });
}

function note_edit_body(block)
{
	var id_all = $(block).parent().parent().attr('id').split('_') // gets record id from parent container id
	var id = id_all[2] // id of the record in database
	
	var content = trim($('#nsbs_'+id).html()) // gets content
	
	var newform = "<textarea class=\"note_edit_body\" rows=\"4\" style=\"width:100%\""
				+"id=\"nsbe_"+id+"\">" // nsbe = note_small_body_edit
				+content+"</textarea><br />"
				+ "<input type=\"submit\" value=\"Save &#x2192;\" "
				+ "onclick=\"note_save_body('"+id+"')\">"
		
	$(block).remove()
	$('#nsbs_'+id).replaceWith(newform)
}

function note_save_body(id)
{
	var content = $('#nsbe_'+id).val() // it's val() even though textarea doesn't have a value...
	
	$.ajax({
		type: "POST",
		url: "controllers/notes_controller.php",
		data: {action:'field_update', id:id, field:'body', value:content},
		success: function(msg){
			var output = msg + "&nbsp;<a href=\"#\" onclick=\"note_edit_body(this)\">[Edit]</a>"
			$('#ns_body_'+id).html(output)
		}
	 });
}